﻿QQ = new (function () {
    var userList = {};
    var onlineCount = 0;
    function UserInfo(uid, nickName, status) {
        this.uid = uid;
        this.nickName = nickName;
        this.status = Number(status);
    }
    UserInfo.prototype.ONLINE = 0;
    UserInfo.prototype.BUSY = 1;
    UserInfo.prototype.AWAY = 2;
    UserInfo.prototype.OFFLINE = 3;

    function addUser(data, isFullData) {
        if (isFullData) {
            userList = {};
            onlineCount = 0;
        }
        onlineCount += data.length;
        $('#EQQ_Class_online_OnlineCounter').html(onlineCount);

        var html = '';
        for (var i = 0; i < data.length; i++) {
            var userInfo = data[i];
            userList[userInfo.uid] = userInfo;

            html += '<div class="EQQ_BuddyList_Buddy" style="background-color: transparent;" id="div_User_' + userInfo.uid + '">                ';
            html += '                                                    <div class="EQQ_BuddyList_ClientType">                                 ';
            html += '                                                    </div>                                                                 ';
            html += '                                                    <div class="EQQ_BuddyList_AvatarContainer">                            ';
            html += '                                                        <img class="EQQ_BuddyList_Avatar" src="./qq_files/getface(1).png"> ';
            html += '                                                        <div class="EQQ_BuddyList_State">                                  ';
            html += '                                                        </div>                                                             ';
            html += '                                                    </div>                                                                 ';
            html += '                                                    <div class="EQQ_BuddyList_RightContainer">                             ';
            html += '                                                        <div class="EQQ_BuddyList_Nick ">                                  ';
            html += '                                                            ' + userInfo.nickName + '</div>                                ';
            html += '                                                        <div title="" class="EQQ_BuddyList_Sign">                          ';
            html += '                                                        </div>                                                             ';
            html += '                                                    </div>                                                                 ';
            html += '                                                </div>                                                                     ';
        }

        if (isFullData) {
            $('#EQQ_Class_online_online').html(html);
        } else {
            $('#EQQ_Class_online_online').append(html);
        }
    }

    this.loadUserList = function (data) {
        var list = data.split('#');
        var ar = [];
        for (var i = 0; i < list.length; i++) {
            var item = list[i].split('|');
            ar.push(new UserInfo(item[0], item[1], item[2]));
        }

        addUser(ar, true);
    };

    this.incUser = function (data) {
        var item = data.split('#');
        var userInfo = new UserInfo(item[0], item[1], item[2]);
        if (userInfo.status === userInfo.ONLINE) {
            addUser([userInfo], false);
        } else {
            userList[userInfo.uid] = null;
            onlineCount -= 1;
            $('#div_User_' + userInfo.uid).remove();
        }
    }
});